package com.project.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.entity.po.ReserveNumberRelation;
import com.project.entity.po.TableNumber;
import com.project.entity.po.TableReserve;
import com.project.entity.po.User;
import com.project.entity.qo.TableNumberQo;
import com.project.entity.vo.TableNumberVo;
import com.project.entity.vo.TableReserveVo;
import com.project.exception.ServiceException;
import com.project.mapper.ReserveNumberRelationMapper;
import com.project.mapper.TableNumberMapper;
import com.project.mapper.TableReserveMapper;
import com.project.entity.qo.TableReserveQo;
import com.project.result.ResponseResult;
import com.project.result.ResultCode;
import com.project.service.TableNumberService;
import com.project.service.TableReserveService;
import com.project.utils.OrderUtils;
import com.project.utils.ShiroUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author chen
 * @date 2022/3/29
 */
@SuppressWarnings("all")
@Service
public class TableReserveServiceImpl implements TableReserveService {
    public static final int CALL_OFF = 2;
    @Resource
    private TableReserveMapper tableReserveMapper;

    @Resource
    private ReserveNumberRelationMapper reserveNumberRelationMapper;

    @Resource
    private TableNumberService tableNumberService;


    @Resource
    private OrderUtils orderUtils;

    TableReserve tableReserve = new TableReserve();

    /**
     * 添加桌位预定信息
     * @param tableReserveQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> addTableReserve(TableReserveQo tableReserveQo) {
        //获取订单编号
        String orderNumber = orderUtils.getOrderNumber();
        tableReserveQo.setOrderNumber(orderNumber);
        //获取就餐时间
        Date dineTime = tableReserveQo.getDineTime();
        //设置过期时间 为就餐时间三十分钟后
        DateTime dateTime = DateUtil.offsetMinute(dineTime, 30);
        tableReserveQo.setEndTime(dateTime);

        //qo 转 po 对象
        BeanUtils.copyProperties(tableReserveQo,tableReserve);
        //执行插入操作 
        int insert = tableReserveMapper.insert(tableReserve);
        //判断是否插入成功
        if(insert == 0){
            throw new ServiceException(ResultCode.SYSTEM_UNKNOW_ERROR);
        }
        //获取当前订单编号的id
        TableReserve tableReserve = tableReserveMapper.selectReserveIdByOrderNumber(orderNumber);
        //根据订单编号获取预约信息的id
        ReserveNumberRelation reserveNumberRelation = new ReserveNumberRelation();
        reserveNumberRelation.setTableNumber(tableReserveQo.getTaberNum());
        reserveNumberRelation.setReserveId(tableReserve.getReserveId());
        //中间表添加
        int i = reserveNumberRelationMapper.insertAll(reserveNumberRelation);
        if(i == 0){
            throw new ServiceException(ResultCode.SYSTEM_UNKNOW_ERROR);
        }
        //预约成功修改该座位状态为已使用
        tableNumberService.updateTableStatus(tableReserveQo.getTaberNum(),0);
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public ResponseResult<List<TableReserveVo>> getTableReserve() {
        QueryWrapper<TableReserve> qw = new QueryWrapper<>();
        qw.lambda().eq(TableReserve::getIsDelete,1);
        List<TableReserve> tableReserves = tableReserveMapper.selectList(qw);
        if(tableReserves == null){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        List<TableReserveVo> tableReserveVoList = new ArrayList<>();
        for (TableReserve t:tableReserves) {
            TableReserveVo tableReserveVo = new TableReserveVo();
            BeanUtils.copyProperties(t,tableReserveVo);
            tableReserveVoList.add(tableReserveVo);
        }
        return ResponseResult.success(ResultCode.SUCCESS,tableReserveVoList);
    }

    /**
     * 查询符合条件的
     * @param tableReserveQo
     * @return
     */
    @Override
    public ResponseResult<List<TableReserveVo>> getByTableReserve(TableReserveQo tableReserveQo) {

        QueryWrapper<TableReserve> qw = new QueryWrapper<>();
        qw.lambda().eq(TableReserve::getReserveId,tableReserveQo.getReserveId()).or()
                .eq(TableReserve::getPhone,tableReserveQo.getPhone()).or()
                .eq(TableReserve::getUsername,tableReserveQo.getUsername()).or()
                .eq(TableReserve::getPersons,tableReserveQo.getPersons()).or()
                .eq(TableReserve::getStatus,tableReserveQo.getStatus()).or()
                .eq(TableReserve::getOrderNumber,tableReserveQo.getOrderNumber());
        List<TableReserve> tableReserves = tableReserveMapper.selectList(qw);
        if(tableReserves == null){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        List<TableReserveVo> tableReserveVoList = new ArrayList<>();
        for (TableReserve t:tableReserves) {
            TableReserveVo tableReserveVo = new TableReserveVo();
            BeanUtils.copyProperties(t,tableReserveVo);
            tableReserveVoList.add(tableReserveVo);
        }
        return ResponseResult.success(ResultCode.SUCCESS,tableReserveVoList);
    }

    /**
     * 根据预定的id修改预定信息
     * @param tableReserveQo
     * @return
     */
    @Override
    public ResponseResult<ResultCode> updateTableReserve(TableReserveQo tableReserveQo) {
        BeanUtils.copyProperties(tableReserveQo,tableReserve);
        int i = tableReserveMapper.updateById(tableReserve);
        if(i == 0){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 删除
     * @param reserveId
     * @return
     */
    @Override
    public ResponseResult<ResultCode> delTableReserve(Long reserveId) {
        int i = tableReserveMapper.delTableReserve(reserveId);
        if(i == 0){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 修改订单状态
     * @param status
     * @param reserveId
     * @return
     */
    @Override
    public ResponseResult<ResultCode> updateStatus(Integer status, long reserveId) {
        //判断要修改的状态是不是取消
        if (status == CALL_OFF){
            //获取要取消预约订单绑定的桌号信息
            TableNumber tableNumber = tableReserveMapper.selectTableNumberByReserveId(reserveId);
            //修改该座位状态为未使用
            tableNumberService.updateTableStatus(tableNumber.getTableNumber(),1);
        }
        int i = tableReserveMapper.updateStatus(status, reserveId);
        if(i == 0){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        return ResponseResult.success(ResultCode.SUCCESS);
    }

    /**
     * 查询预约的桌位信息
     * @param reserveId
     * @return
     */
    @Override
    public ResponseResult<TableNumberVo> selectTableNumberByReserveId(Long reserveId) {
        TableNumber tableNumber = tableReserveMapper.selectTableNumberByReserveId(reserveId);
        if(tableNumber == null){
            throw new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        TableNumberVo tableNumberVo = new TableNumberVo();
        BeanUtils.copyProperties(tableNumber, tableNumberVo);
        return ResponseResult.success(ResultCode.SUCCESS,tableNumberVo);
    }

    /**
     * 分页查询
     * @param current
     * @return
     */
    @Override
    public ResponseResult<List<TableReserveVo>> selectTableReservePage(Integer current) {
        Page<TableReserve> page = new Page<>(current,3);
        QueryWrapper<TableReserve> qw = new QueryWrapper<>();
        qw.lambda().eq(TableReserve::getIsDelete,1);
        Page<TableReserve> tableReservePage = tableReserveMapper.selectPage(page, qw);
        if(ObjectUtils.isEmpty(tableReservePage)){
            throw  new ServiceException(ResultCode.SYSTEM_INNER_ERROR);
        }
        //获取当前页数据
        List<TableReserve> tableReserveList = tableReservePage.getRecords();

        List<TableReserveVo> tableReserveVoList = new ArrayList<>();
        for (TableReserve t:tableReserveList) {
            TableReserveVo tableReserveVo = new TableReserveVo();
            BeanUtils.copyProperties(t,tableReserveVo);
            tableReserveVoList.add(tableReserveVo);
        }
        return ResponseResult.success(ResultCode.SUCCESS,tableReserveVoList);
    }


}
